package cn.wk.service.impl;

import cn.wk.domain.Employee;
import cn.wk.domain.Tenant;
import cn.wk.mapper.EmployeeMapper;
import cn.wk.mapper.TenantMapper;
import cn.wk.service.IEmployeeService;
import cn.wk.util.AjaxResult;
import cn.wk.vo.UserContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2021-03-02
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private TenantMapper tenantMapper;

    /**
     * 根据loginId来查找用户和部门
     * @param loginId
     * @return
     */
    @Override
    public AjaxResult selectLogin(Long loginId) {
        Employee dbEmployee = baseMapper.selectByLoginId(loginId);
        System.out.println(dbEmployee.getTenantId());
        Tenant tenant = tenantMapper.selectById(dbEmployee.getTenantId());
        //给usercontext封装
        UserContext userContext = new UserContext(dbEmployee.getTenantId(),
                tenant.getCompanyName(), dbEmployee.getId(), dbEmployee.getUsername());
        return AjaxResult.me().setResultObj(JSON.toJSONString(userContext));
    }
}
